package com.weeego.web.base;

import com.weeego.util.file.MakeDir;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Random;

import org.apache.struts2.ServletActionContext;

public class UpLoadPhotoAction extends BaseAction {

	/**
	 * 
	 */

	private static final long serialVersionUID = 1L;

	private String newsImgUrl;
	private String comeform;
	private String callFun;
	private File upload_file;
	private String upload_fileContentType;

	private String upload_fileFileName;
	private String directory;

	public String getDirectory() {
		return directory;
	}

	public void setDirectory(String directory) {
		this.directory = directory;
	}

	public File getUpload_file() {

		return upload_file;
	}

	public void setUpload_file(File upload_file) {
		this.upload_file = upload_file;
	}

	public String getUpload_fileContentType() {
		return upload_fileContentType;
	}

	public void setUpload_fileContentType(String upload_fileContentType) {
		this.upload_fileContentType = upload_fileContentType;
	}

	public String getUpload_fileFileName() {
		return upload_fileFileName;
	}

	public void setUpload_fileFileName(String upload_fileFileName) {
		this.upload_fileFileName = upload_fileFileName;
	}

	public String form() {
		return "success";
	}

	@Override
	public String action() {
		if ("".equals(upload_file)) {
			return SUCCESS;
		}

		// String schemePath =
		// ServletActionContext.getRequest().getRealPath("/");
		String schemePath = "";
		String rootPath = directory + File.separator + FormatDate.getNowDay()
				+ File.separator;
		String currPath = schemePath + rootPath;

		logger
				.info("-----------------currPath------1111111----------------------"
						+ currPath);

		// String currPath = getText("struts.messages.upload.path");
		// currPath = currPath + File.separator;

		MakeDir _MakeDir = new MakeDir();

		_MakeDir.makeDir(currPath);

		try {
			FileInputStream in = new FileInputStream(upload_file);
			File diskFile = new File(currPath + upload_fileFileName);
			FormatDate _FormatDate = new FormatDate();
			Random random = new Random();
			String _temp = _FormatDate.getNowTime() + random.nextInt(9999);
			int point = upload_fileFileName.lastIndexOf(".");
			String newFileName = _temp + upload_fileFileName.substring(point);

			upload_fileFileName = newFileName;

			diskFile = new File(currPath + upload_fileFileName);
			diskFile.createNewFile();
			FileOutputStream out = new FileOutputStream(diskFile);
			byte buffer[] = new byte[1024];
			while ((in.read(buffer)) != -1) {
				out.write(buffer);
			}
			in.close();
			out.close();

			// ImageEdit.resize_scale(currPath + upload_fileFileName, currPath
			// + "min_" + upload_fileFileName, 800, 600);

			// 添加水印
			// ImageUtils.pressImage("E:/struts2/etaocn.jpg", currPath
			// + upload_fileFileName, 20, 20);

		} catch (Exception e) {
			logger.error("====upload file has some error: ", e);
		}
		newsImgUrl = File.separator + rootPath + upload_fileFileName;
		newsImgUrl = newsImgUrl.replace("\\", "/");
		return SUCCESS;
	}

	public String getNewsImgUrl() {
		return newsImgUrl;
	}

	public void setNewsImgUrl(String newsImgUrl) {
		this.newsImgUrl = newsImgUrl;
	}

	public String getComeform() {
		return comeform;
	}

	public void setComeform(String comeform) {
		this.comeform = comeform;
	}

	public String getCallFun() {
		return callFun;
	}

	public void setCallFun(String callFun) {
		this.callFun = callFun;
	}

}
